/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UpdateKaryawan;

import Koneksi.KoneksiDatabase;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import Karyawan.Karyawan;

/**
 *
 * @author yolan
 */
public class updateKaryawanKontrol {

    private Connection conn;

    public updateKaryawanKontrol(Connection conn2) {
        this.conn = conn2;
    }

    public static updateKaryawanKontrol getKoneksi() throws SQLException {
        KoneksiDatabase konDB = new KoneksiDatabase();
        updateKaryawanKontrol katkontrol;
        katkontrol = new updateKaryawanKontrol(KoneksiDatabase.getConn());
        return katkontrol;
    }

    public void tambahKategori(Karyawan karyawan) throws SQLException {
        String nip = karyawan.getNIP();
        String nama = karyawan.getNama();
        String golongan = karyawan.getGolongan();
        String status = karyawan.getStatus();
       String hadir=karyawan.getJumlahhadir();
        Statement statement = conn.createStatement();
        String sql = "insert into karyawan values('" + nip + "','" + nama + "','" + golongan + "','" + status +"','"+hadir+"')";

        statement.executeUpdate(sql);
        conn.close();
    }

    public List<Karyawan> getAllKategori() throws SQLException {

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Karyawan> kategori = new ArrayList<Karyawan>();
        try {
            conn.setAutoCommit(false);
            String sql = "select * from karyawan order by nip";
            preparedStatement = conn.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Karyawan kat = new Karyawan();
                kat.setNIP(resultSet.getString(1));
                kat.setNama(resultSet.getString(2));
                kat.setGolongan(resultSet.getString(3));
                kat.setStatus(resultSet.getString(4));
                kat.setJumlahhadir(resultSet.getString(5));
                kategori.add(kat);//tambah kategori baru
            }
            conn.commit();
        } catch (SQLException ex) {
            Logger.getLogger(updateKaryawanKontrol.class.getName()).log(Level.SEVERE, null, ex);
        }
        return kategori;
    }

    public void updateKartustock(Karyawan stock) throws SQLException {

        PreparedStatement preparedStatement = null;
        try {
            conn.setAutoCommit(true);
            String sql = "update karyawan set  nama = ? , golongan = ? , status = ? where nip= ? ";
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, stock.getNama());
            preparedStatement.setString(2, stock.getGolongan());
            preparedStatement.setString(3, stock.getStatus());
            preparedStatement.setString(4, stock.getNIP());
            preparedStatement.executeUpdate();
            conn.commit();
        } catch (SQLException ex) {
            conn.rollback();
            throw ex;
        } finally {
            try {
                conn.setAutoCommit(true);
            } catch (SQLException ex) {
                Logger.getLogger(updateKaryawanKontrol.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void hapusKategori(Karyawan kategori) throws SQLException {
        PreparedStatement preparedStatement = null;
        try {
            conn.setAutoCommit(false);
            String sql = " delete from karyawan where nip=? ";
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, kategori.getNIP());
            preparedStatement.executeUpdate();
            conn.commit();
        } catch (SQLException ex) {
            conn.rollback();
            throw ex;
        } finally {
            try {
                conn.setAutoCommit(true);
            } catch (SQLException ex) {
                Logger.getLogger(updateKaryawanKontrol.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
